/**
 * 全局共享配置
 */
//导入Cookies 
import Cookies from 'js-cookie'
//导入默认配置
import { menuBg, isCollapse } from '@/settings'

const state = {
	//左侧菜单栏配置
	sidebar:{
		//折叠状态
		isCollapse: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : isCollapse,
		//菜单默认背景色
		background: Cookies.get('background') ? Cookies.get('background') : menuBg
	}
}

const mutations = {
	//配置菜单栏状态
	SET_SIDEBAR: state => {
		state.sidebar.isCollapse = !state.sidebar.isCollapse
		//缓存做处理
		if (state.sidebar.isCollapse) {
			Cookies.set('sidebarStatus', 1)
		} else {
			Cookies.set('sidebarStatus', 0)
		}
	},
	//配置背景色
	SET_BG: (state,background) => {
		//设置背景色
		state.sidebar.background = background
		if (state.sidebar.background) {
			Cookies.set('background', background)
		} else {
			Cookies.set('background', menuBg)
		}
	}
}

const actions = {
	//设置菜单栏状态
	setMenu( {commit}){
		return new Promise((resolve, reject) => {
			commit('SET_SIDEBAR')
			resolve()
		}).catch(error => {
			reject(error)
		})
	},
	//设置背景色
	setBg( {commit}, background){
		return new Promise((resolve, reject) => {
			commit('SET_BG',background)
			resolve()
		}).catch(error => {
			reject(error)
		})
	}
}

//暴露方法
export default {
  namespaced: true,
  state,
  mutations,
  actions
}